package com.java.comprehensive.other.sort;

/**
 * @Author: Dgg
 * @Date: 2021/10/20 20:10
 */

class ListNode {
	int val;
	ListNode next;

	ListNode() {
	}

	ListNode(int val) {
		this.val = val;
	}

	ListNode(int val, ListNode next) {
		this.val = val;
		this.next = next;
	}

	@Override
	public String toString() {
		return "ListNode{" +
				"val=" + val +
				", next=" + next +
				'}';
	}
}


public class NumToSum {

	public static void main(String[] args) {
		NumToSum numToSum = new NumToSum();
		ListNode listNode1 = new ListNode(2);
		ListNode listNode2 = new ListNode(4);
		ListNode listNode3 = new ListNode(3);
		ListNode listNode4 = new ListNode(5);
		ListNode listNode5 = new ListNode(6);
		ListNode listNode6 = new ListNode(4);
		listNode2.next = listNode3;
		listNode1.next = listNode2;
		ListNode l1 = listNode1;
		listNode5.next = listNode6;
		listNode4.next = listNode5;
		ListNode l2 = listNode4;
		System.out.println(numToSum.addTowNumSum(l1, l2));

	}

	public ListNode addTowNumSum(ListNode l1, ListNode l2) {
		ListNode pre = new ListNode(0);
		ListNode cur = pre;
		int carry = 0;
		while(l1 != null || l2 != null) {
			int x = l1 == null ? 0 : l1.val;
			int y = l2 == null ? 0 : l2.val;
			int sum = x + y + carry;

			carry = sum / 10;
			sum = sum % 10;
			cur.next = new ListNode(sum);

			cur = cur.next;
			if(l1 != null)
				l1 = l1.next;
			if(l2 != null)
				l2 = l2.next;
		}
		if(carry == 1) {
			cur.next = new ListNode(carry);
		}
		return pre.next;
	}
}

/*
	public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
		ListNode pre = new ListNode(0);
		ListNode cur = pre;
		int carry = 0;
		while(l1 != null || l2 != null) {
			int x = l1 == null ? 0 : l1.val;
			int y = l2 == null ? 0 : l2.val;
			int sum = x + y + carry;

			carry = sum / 10;
			sum = sum % 10;
			cur.next = new ListNode(sum);

			cur = cur.next;
			if(l1 != null)
				l1 = l1.next;
			if(l2 != null)
				l2 = l2.next;
		}
		if(carry == 1) {
			cur.next = new ListNode(carry);
		}
		return pre.next;
	}
}
*/

